package net.blank.ui
{
	import net.blank.dataTransformation.DataTransform;
	
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	import flash.net.FileReference;
	import flash.ui.Keyboard;
	import flash.utils.ByteArray;
	import flash.utils.Dictionary;
	
	/**
	 * 开发者数据查看器
	 * @author blank
	 * 
	 */
	public class DeveloperLooker extends Sprite
	{
		/**
		 * 用户按键列表
		 */
		private var keyList:Vector.<int>;
		
		/**
		 * 获取开始者数据的按键列表
		 */
		private var standardKeyList:Vector.<int>;
		
		/**
		 * 静态实例
		 */
		private static var $instance:DeveloperLooker = new DeveloperLooker();
		
		/**
		 * 需要打印的ID列表
		 */
		private static var needPrintIds:Dictionary=new Dictionary();
		
		/**
		 * 开发者数据
		 */
		private static var data:ByteArray=new ByteArray();
		
		/**
		 * 是否压缩数据
		 */
		public static var isCompress:Boolean = true;
		
		/**
		 * 保存完成
		 * @param event
		 * 
		 */
		private function saveComplete(event:Event):void{
			var f:FileReference = event.target as FileReference;
			f.removeEventListener(Event.COMPLETE, saveComplete);
			f.removeEventListener(Event.CANCEL, cancelSave);
			data.clear();
		}
		
		/**
		 * 取消保存
		 * @param event
		 * 
		 */
		private function cancelSave(event:Event):void{
			var f:FileReference = event.target as FileReference;
			f.removeEventListener(Event.COMPLETE, saveComplete);
			f.removeEventListener(Event.CANCEL, cancelSave);
		}
		
		/**
		 * 保存数据
		 * 
		 */
		private function saveData():void{
			var f:FileReference = new FileReference();
			f.addEventListener(Event.COMPLETE, saveComplete);
			f.addEventListener(Event.CANCEL, cancelSave);
			f.save(data, "developerData.dat");
		}
		
		/**
		 * 按下键盘,与列表对比
		 * @param event
		 * 
		 */
		private function keyDownHandler(event:KeyboardEvent):void{
			keyList.push(event.keyCode);
			if (keyList.length > standardKeyList.length){
				keyList.shift();
			}
			if (keyList.length == standardKeyList.length){
				if (keyList.toString() == standardKeyList.toString()){
					saveData();
				}
			}
		}
		
		/**
		 * 点击开始者工具
		 * @param event
		 * 
		 */
		private function clickHandler(event:MouseEvent):void{
			if (stage){
				stage.focus = this;
				addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);
			}
		}
		
		/**
		 * 开发者数据查看器
		 * 
		 */
		public function DeveloperLooker(){
			tabEnabled = true;
			keyList = new Vector.<int>;
			standardKeyList = Vector.<int>([
				Keyboard.UP,
				Keyboard.DOWN, 
				Keyboard.UP,
				Keyboard.LEFT, 
				Keyboard.RIGHT
			]);
			
			graphics.beginFill(16711680, 0);
			graphics.drawRect(0, 0, 20, 20);
			graphics.endFill();
			
			addEventListener(MouseEvent.CLICK, clickHandler);
		}
		
		/**
		 * 添加需要打印的ID
		 * @param ids
		 * 
		 */
		private function addPrintIds(...ids):void{
			for each(var id:int in ids){
				needPrintIds[id]=true;
			}
		}
		
		/**
		 * 移除需要打印的ID
		 * @param ids
		 * 
		 */
		private function removePrintIds(...ids):void{
			for each(var id:int in ids){
				delete needPrintIds[id];
			}
		}
		
		/**
		 * 打印调试数据
		 * @param id
		 * @param isSave
		 * @param args
		 * 
		 */
		public static function print(id:int,isSave:Boolean,...args):void{
			var str:String = "";
			for each(str in args){
				str +="\t" + str.toString();
			}
			
			if(id in needPrintIds){
				trace(str);
			}
			
			if(isSave){
				var b:* = new ByteArray();
				b.writeUTFBytes(str);
				
				DataTransform.writeElement(b, data, 0);
			}
		}
		
		/**
		 * 保存字符串
		 * @param isTrace
		 * @param args
		 * 
		 */
		public static function saveStr(isPrint:Boolean, ... args):void{
			var str:String = "";
			if(args){
				for each(str in args){
					str +="\t" + str.toString();
				}
				
				!isPrint || trace(str);
				
				var b:* = new ByteArray();
				b.writeUTFBytes(str);
				
				DataTransform.writeElement(b, data, 0);
			}
		}
		
		/**
		 * 添加二进制数据
		 * @param data
		 * 
		 */
		public static function addBinaryData(data:ByteArray):void{
			DataTransform.writeElement(data, data, 1);
		}
	}
}